package network;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JTextField;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.ImageIcon;

public class ServerUI extends JFrame implements Runnable{
	
	ServerSocket server;
	Socket client;
	boolean threadFlag = true;
	StringBuilder data = new StringBuilder();
	BufferedReader receive = null;
	BufferedWriter send = null;
	String youChioce;
	String Ch;
	String yf;

	private JPanel contentPane;
	private JButton bt;
	private JButton bt1;
	private JButton bt2;
	private JLabel lblNewLabel;
	private JButton bt3;
	private JButton bt4;
	private JButton bt5;
	private JTextField textField;
	private JLabel lblNewLabel_1;
	private JLabel lblNewLabel_2;
	private JLabel lblNewLabel_3;
	private JLabel lblNewLabel_4;
	private JLabel lblNewLabel_5;
	private JLabel lblNewLabel_6;
	private JLabel lblNewLabel_7;
	private JLabel lblNewLabel_8;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ServerUI frame = new ServerUI();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public ServerUI() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 630, 431);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getBt());
		contentPane.add(getBt1());
		contentPane.add(getBt2());
		contentPane.add(getLblNewLabel());
		contentPane.add(getBt3());
		contentPane.add(getBt4());
		contentPane.add(getBt5());
		contentPane.add(getTextField());
		contentPane.add(getLblNewLabel_1());
		contentPane.add(getLblNewLabel_2());
		contentPane.add(getLblNewLabel_3());
		contentPane.add(getLblNewLabel_4());
		contentPane.add(getLblNewLabel_5());
		contentPane.add(getLblNewLabel_6());
		contentPane.add(getLblNewLabel_7());
		contentPane.add(getLblNewLabel_8());
		
		try {
			InetAddress ia = InetAddress.getLocalHost();
			setTitle("서버: IP" + ia.getHostAddress() + ", port: " + "4444");

		} catch (Exception e) {

		}
		
	}
	public void gameStart(){
		try{
			bt3.setEnabled(true);
			bt4.setEnabled(true);
			bt5.setEnabled(true);
			
			
		}catch(Exception e){
			
		}
	}
	
	public void sendMsg(String s) {
		try {
			send.write(s);
			send.newLine();
			send.flush();
			//dataSet("<font color = 'blue'> 서버 :" + s + "</font>");
		} catch (Exception e) {

		}
	}
	
	public void serverStart() {
		try {
			Thread t = new Thread(this);
			t.start();
			
			bt.setEnabled(false);
			bt1.setEnabled(true);
			
			
			
			
			dataSet("<b>서버가 시작 되었네 그려~ </b>");	
		} catch (Exception e) {

		}
	}

	public void serverStop() {
		bt.setEnabled(true);
		bt1.setEnabled(false);
		threadFlag = false; 
		
		try {

		} catch (Exception e) {

		}

	}

	public void dataSet(String d) {
		try {
			data.append(d+"<br/>");
			//result.setText(data.toString());

		} catch (Exception e) {

		}

	}

	public void run() {
		try {
			
			server = new ServerSocket(4444);
			client = server.accept();
			bt2.setEnabled(true);
	
			
			
			OutputStream os = client.getOutputStream();
			OutputStreamWriter osw = new OutputStreamWriter(os);
			send = new BufferedWriter(osw);
			
			InputStream is = client.getInputStream();
			InputStreamReader isr = new InputStreamReader(is);
			receive = new BufferedReader(isr);

			threadFlag = true;
			
			sendMsg("1");
			
			
			while(threadFlag){
				String str = receive.readLine();
//				dataSet("수신: " + str);
				if(str.equals("145")){
					lblNewLabel_2.setIcon(new ImageIcon("H:\\JAVA\\ht\\1403\\145.bmp"));
				
				}
				youChioce =str;
				if(str.equals("가위")){
					lblNewLabel_4.setIcon(new ImageIcon("H:\\JAVA\\ht\\1403\\가위.jpg"));
				}else if(str.equals("바위")){
					lblNewLabel_4.setIcon(new ImageIcon("H:\\JAVA\\ht\\1403\\바위.png"));
				}else if(str.equals("보")){
					lblNewLabel_4.setIcon(new ImageIcon("H:\\JAVA\\ht\\1403\\보.jpg"));
					
				}
				
			}
			
			dataSet("<font color = 'red' > 서버가 중지 되었네 그려~ </font>");
			bt.setEnabled(true);
			bt1.setEnabled(false);
			
			receive.close();
			isr.close();
			is.close();
			
			send.close();
			osw.close();
			os.close();
			
			client.close();
			server.close();
			
		} catch (Exception e) {

		}

	}
	
	
	
	
	private JButton getBt() {
		if (bt == null) {
			bt = new JButton("게임 서버 시작");
			bt.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					serverStart();
				}
			});
			bt.setBounds(48, 31, 124, 23);
		}
		return bt;
	}
	private JButton getBt1() {
		if (bt1 == null) {
			bt1 = new JButton("게임 서버 종료");
			bt1.setEnabled(false);
			bt1.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					serverStop();
				}
			});
			bt1.setBounds(247, 31, 124, 23);
		}
		return bt1;
	}
	private JButton getBt2() {
		if (bt2 == null) {
			bt2 = new JButton("게임시작");
			bt2.setEnabled(false);
			bt2.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					gameStart();
					bt2.setEnabled(false);
					
				}
			});
			bt2.setBounds(444, 31, 124, 23);
		}
		return bt2;
	}
	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("게임 전적 :");
			lblNewLabel.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel.setBounds(171, 79, 75, 15);
		}
		return lblNewLabel;
	}
	private JButton getBt3() {
		if (bt3 == null) {
			bt3 = new JButton("가위");
			bt3.setEnabled(false);
			bt3.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {

					lblNewLabel_3.setIcon(new ImageIcon("H:\\JAVA\\ht\\1403\\가위.jpg"));
					sendMsg("가위");
					bt4.setEnabled(false);
					bt5.setEnabled(false);
				}
			});
			bt3.setBounds(48, 317, 124, 23);
		}
		return bt3;
	}
	private JButton getBt4() {
		if (bt4 == null) {
			bt4 = new JButton("바위");
			bt4.setEnabled(false);
			bt4.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					lblNewLabel_3.setIcon(new ImageIcon("H:\\JAVA\\ht\\1403\\바위.png"));
					sendMsg("바위");
					bt3.setEnabled(false);
					bt5.setEnabled(false);
				}
			});
			bt4.setBounds(247, 317, 124, 23);
		}
		return bt4;
	}
	private JButton getBt5() {
		if (bt5 == null) {
			bt5 = new JButton("보");
			bt5.setEnabled(false);
			bt5.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					lblNewLabel_3.setIcon(new ImageIcon("H:\\JAVA\\ht\\1403\\보.jpg"));
					sendMsg("보");
					bt3.setEnabled(false);
					bt4.setEnabled(false);
				}
			});
			bt5.setBounds(444, 317, 124, 23);
		}
		return bt5;
	}
	private JTextField getTextField() {
		if (textField == null) {
			textField = new JTextField();
			textField.setEditable(false);
			textField.setHorizontalAlignment(SwingConstants.CENTER);
			textField.setText("  전  승  패  무");
			textField.setBounds(247, 75, 200, 23);
			textField.setColumns(10);
		}
		return textField;
	}
	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("");
			lblNewLabel_1.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel_1.setIcon(new ImageIcon("H:\\JAVA\\ht\\1403\\1.bmp"));
			lblNewLabel_1.setBounds(48, 142, 97, 61);
		}
		return lblNewLabel_1;
	}
	private JLabel getLblNewLabel_2() {
		if (lblNewLabel_2 == null) {
			lblNewLabel_2 = new JLabel("");
			lblNewLabel_2.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel_2.setBounds(48, 246, 97, 61);
		}
		return lblNewLabel_2;
	}
	private JLabel getLblNewLabel_3() {
		if (lblNewLabel_3 == null) {
			lblNewLabel_3 = new JLabel("New label");
			lblNewLabel_3.setBounds(171, 161, 200, 146);
		}
		return lblNewLabel_3;
	}
	private JLabel getLblNewLabel_4() {
		if (lblNewLabel_4 == null) {
			lblNewLabel_4 = new JLabel("New label");
			lblNewLabel_4.setBounds(385, 161, 200, 146);
		}
		return lblNewLabel_4;
	}
	private JLabel getLblNewLabel_5() {
		if (lblNewLabel_5 == null) {
			lblNewLabel_5 = new JLabel("접속자");
			lblNewLabel_5.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel_5.setBounds(450, 122, 57, 15);
		}
		return lblNewLabel_5;
	}
	private JLabel getLblNewLabel_6() {
		if (lblNewLabel_6 == null) {
			lblNewLabel_6 = new JLabel("나");
			lblNewLabel_6.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel_6.setBounds(240, 122, 57, 15);
		}
		return lblNewLabel_6;
	}
	private JLabel getLblNewLabel_7() {
		if (lblNewLabel_7 == null) {
			lblNewLabel_7 = new JLabel("내 이미지");
			lblNewLabel_7.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel_7.setBounds(58, 122, 75, 15);
		}
		return lblNewLabel_7;
	}
	private JLabel getLblNewLabel_8() {
		if (lblNewLabel_8 == null) {
			lblNewLabel_8 = new JLabel("상대 이미지");
			lblNewLabel_8.setHorizontalAlignment(SwingConstants.CENTER);
			lblNewLabel_8.setBounds(58, 221, 75, 15);
		}
		return lblNewLabel_8;
	}

	
	
}
